/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/09/04
*/
#include "TableLayout.h"
#include "../edui.h"

EDUI_NAMESPACE_BEGIN

TableLayout::TableLayout() : m_iSelected(0)
{}

TableLayout::~TableLayout()
{}

TableLayout* TableLayout::GetInterface()
{
    TableLayout* tableLayout = new TableLayout;
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[TableLayout] new tableLayout : %d", tableLayout);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Add("TableLayout", tableLayout);
#endif
    return tableLayout;
}

void TableLayout::Release()
{
    this->Clear();
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[TableLayout] delete tableLayout : %s, %d", this->GetName(), this);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Remove(this);
#endif
    delete this;
}

TableLayout* TableLayout::ToTableLayout(Control* ctrl)
{
    return dynamic_cast<TableLayout*>(ctrl);
}

size_t TableLayout::GetSelected()
{
    return m_iSelected;
}

bool TableLayout::SetSelected(size_t iSelected)
{
    if ((iSelected < 0) || (iSelected >= m_items->size())) return false;
    if (iSelected == m_iSelected) return true;
    m_iSelected = iSelected;
    this->Invaldate();
    this->evt.selectedChanged.emit(this, m_iSelected);
    return true;
}

Control* TableLayout::FindControlByPt(int x, int y, Uint32 flag)
{
    if (!Control::FindControlByPt(x, y, flag)) return 0;
    Control* ctrl = this->GetByIndex(m_iSelected);
    if (ctrl){
        ctrl = ctrl->FindControlByPt(x, y, flag);
        if (ctrl) return ctrl;
    }
    return this;
}

bool TableLayout::HandleMsg(Msg* msg)
{
    Control* ctrl = this->GetByIndex(m_iSelected);
    if (ctrl && ctrl->IsVisible() && ctrl->IsEnabled()){
        if (ctrl->HandleMsg(msg)){
            return true;
        }
    }
    return Control::HandleMsg(msg);
}

void TableLayout::Render(IRender* render, const Rect* rc)
{
    Rect oldclip = render->GetClip();

    if (rc) render->SetClip(rc);
    else render->SetClip(&m_rc);

    this->RenderBkColor(render, rc);
    this->RenderBkImages(render, rc);
    this->RenderStatusImages(render, rc);
    this->RenderForeImages(render, rc);
    this->RenderText(render, rc);
    this->RenderBorder(render, rc);

    Control* ctrl = this->GetByIndex(m_iSelected);
    if (ctrl && ctrl->IsVisible() && ctrl->NeedRender(rc)){
        ctrl->Render(render, rc);
    }

    render->SetClip(&oldclip);
}

void TableLayout::AutoLayout()
{
    if (m_wnd->GetRoot() == this){
        m_rc.Reset(0, 0, m_wnd->GetWidth(), m_wnd->GetHeight());
    }
    Control::AutoLayout();

    if (m_items->size() == 0) return;

    Rect rc = m_rc;
    rc.x += m_rcInset.l;
    rc.y += m_rcInset.t;
    rc.w -= (m_rcInset.l + m_rcInset.r);
    rc.h -= (m_rcInset.t + m_rcInset.b);

    Control* ctrl = this->GetByIndex(m_iSelected);
    if (ctrl && ctrl->IsVisible()){
        ctrl->SetRect(&rc);
        ctrl->AutoLayout();
    }
}

void TableLayout::SetAttribute(const char* name, const char* value)
{
    if (0 == strcmp(name, "selectedid")){
        int id = 0;
        ParserInt(value, &id, 0);
        m_iSelected = id;
    } else {
        Container::SetAttribute(name, value);
    }
}

bool TableLayout::ChildCanInvaldate(Control* child)
{
    Control* ctrl = this->GetByIndex(m_iSelected);
    if (!ctrl || child != ctrl) return false;
    Container* parent = Container::ToContainer(this->GetParent());
    if (parent) return parent->ChildCanInvaldate(this);
    return true;
}

EDUI_NAMESPACE_END
